package com.neusoft.neuiotms.dcom.repository.report.impl;

import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.neusoft.bizcore.web.jpa.repository.AbstractRepository;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcom.model.report.QReportTool;
import com.neusoft.neuiotms.dcom.model.report.ReportTool;
import com.neusoft.neuiotms.dcom.repository.report.CustomizedReportToolRepository;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.jpa.impl.JPAQuery;

import io.micrometer.core.instrument.util.StringUtils;

public class CustomizedReportToolRepositoryImpl extends AbstractRepository implements CustomizedReportToolRepository {

    @Override
    public List<ReportTool> index(final Searchable searchable) {
        final QReportTool qReportTool = QReportTool.reportTool;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<ReportTool> query = this.query().selectFrom(qReportTool);
        query.where(where);
        return query.fetch();
    }

    @Override
    public Page<ReportTool> search(final Pageable pageable, final Searchable searchable) {
        final QReportTool qReportTool = QReportTool.reportTool;
        final BooleanBuilder where = this.buildWhere(searchable);
        final JPAQuery<ReportTool> query = this.query().selectFrom(qReportTool);
        query.where(where);
        return super.search(query, pageable);
    }

    @Override
    protected Class<?> getModelClass() {
        return ReportTool.class;
    }

    private BooleanBuilder buildWhere(final Searchable searchable) {

        final BooleanBuilder where = new BooleanBuilder();
        final QReportTool qReportTool = QReportTool.reportTool;

        // 名称检索
        if (!StringUtils.isEmpty(searchable.getStrValue("name"))) {
            where.and(super.containsStr(qReportTool.name, searchable, "name"));
        }

        return where;
    }

}
